﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Infrastructure;
using MSIACCSL2.Web.WarehouseServices;

namespace MSIACCSL2.Web.ReportPage
{
    public partial class TG : System.Web.UI.Page
    {
        private DataAccess<Object> DA;
        private ReportDocument mutasiWarehouse;
        private ReportDocument mutasiWarehouseDetail;
        private string Tipe;

        public TG()
        {
            DA = new DataAccess<object>();
            mutasiWarehouse = new ReportDocument();
            mutasiWarehouseDetail = new ReportDocument();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
            if (IsPostBack)
            {
                LoadReport(Request.QueryString["no"].ToString(), RadioButtonList1.SelectedValue);
            }
        }
      
    
        protected void Button1_Click(object sender, EventArgs e)
        {
          
            LoadReport(Request.QueryString["no"].ToString(), ViewState["tipe"].ToString());

        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ViewState["tipe"] = RadioButtonList1.SelectedValue;
            LoadReport(Request.QueryString["no"].ToString(), ViewState["tipe"].ToString());
        }


      
        public void LoadReport(string NoSO,string tipe)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Select a.NoMutasi,a.TglMutasi,a.[Description], ");
            sb.Append("(Select WarehouseName from Warehouse where KodeWarehouse=a.FromWarehouse) AS FromWarehouse, ");
            sb.Append("(Select WarehouseName from Warehouse where KodeWarehouse=a.ToWarehouse) AS ToWarehouse, ");
            sb.Append("b.SerialNumber,b.Qty,b.Sheet,b.Panjang,b.Lebar,b.KodePallet, ");
            sb.Append("i.ItemName,sg.SubGroupName,it.SatuanUnit as Satuan ");
            sb.Append("from MutasiWarehouse a    ");
            sb.Append("inner Join MutasiWarehouseDetail b on a.NoMutasi = b.NoMutasi      ");
            sb.Append("left Join Inventory it on it.SerialNumber = b.SerialNumber      ");
            sb.Append("left Join Item i on i.ItemId = it.ItemId      ");
            sb.Append("LEFT JOIN SubGroup sg ON sg.SubGroupID = i.SubGroupID      ");
            sb.AppendFormat("Where a.NoMutasi='{0}' Order By sg.SubGroupID,i.ItemId ", NoSO);


            var Source = DA.GetDataListDataSet(sb.ToString());

            if (RadioButtonList2.SelectedValue == "Summary")
            {
                string reportPath = Server.MapPath("~/Report/MutasiWarehouse.rpt");
                mutasiWarehouse.Load(reportPath);
                mutasiWarehouse.SetDataSource(Source.Tables[0]);
                var param = new ParameterDiscreteValue();
                param.Value = tipe;
                var paramValue = new ParameterValues();
                paramValue.Add(param);
                mutasiWarehouse.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
                CrystalReportViewer1.ReportSource = mutasiWarehouse;
            }
            else
            {
                string reportPath = Server.MapPath("~/Report/MutasiWarehouseDetail.rpt");
                mutasiWarehouseDetail.Load(reportPath);
                mutasiWarehouseDetail.SetDataSource(Source.Tables[0]);
                var param = new ParameterDiscreteValue();
                param.Value = tipe;
                var paramValue = new ParameterValues();
                paramValue.Add(param);
                mutasiWarehouseDetail.DataDefinition.ParameterFields["group"].ApplyCurrentValues(paramValue);
                CrystalReportViewer1.ReportSource = mutasiWarehouseDetail;
            }

        }


        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);
            this.Unload += Page_Unload;
        }

        void Page_Unload(object sender, EventArgs e)
        {
            mutasiWarehouse.Clone();
            mutasiWarehouse.Dispose();

            mutasiWarehouseDetail.Clone();
            mutasiWarehouseDetail.Dispose();
        }
      
    }
}